package cn.edu.sgu.www.pms.controller;

import cn.edu.sgu.www.pms.annotation.FeignService;
import cn.edu.sgu.www.pms.consts.CustomConstants;
import cn.edu.sgu.www.pms.consts.CustomMessages;
import cn.edu.sgu.www.pms.consts.MimeTypes;
import cn.edu.sgu.www.pms.dto.MenuTreeDTO;
import cn.edu.sgu.www.pms.dto.UserMenuDTO;
import cn.edu.sgu.www.pms.easyui.Tree;
import cn.edu.sgu.www.pms.restful.JsonResult;
import cn.edu.sgu.www.pms.service.UserMenuService;
import cn.edu.sgu.www.pms.vo.MenuVO;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "用户菜单管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/user_menu", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class UserMenuController {

    private final UserMenuService userMenuService;

    @Autowired
    public UserMenuController(UserMenuService userMenuService) {
        this.userMenuService = userMenuService;
    }

    @ApiOperation("控制菜单显示")
    @RequestMapping(path = "/control", method = RequestMethod.POST)
    public JsonResult<Void> control(MenuTreeDTO menuTreeDTO) {
        userMenuService.control(menuTreeDTO);

        return JsonResult.success(CustomMessages.OPERATE_SUCCESS);
    }

    @ApiOperation("设置菜单排序")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuId", value = "菜单ID", required = true),
            @ApiImplicitParam(name = "pxh", value = "排序号", required = true)
    })
    @RequestMapping(path = "/setSort", method = RequestMethod.POST)
    public JsonResult<Void> setSort(
            @NotBlank(message = "菜单" + CustomMessages.ID_NOT_NULL) String menuId,
            @NotNull(message = "排序号" + CustomMessages.NOT_NULL) Integer pxh) {
        userMenuService.setSort(menuId, pxh);

        return JsonResult.success(CustomMessages.OPERATE_SUCCESS);
    }

    @ApiOperation("查询用户的侧栏菜单")
    @RequestMapping(path = "/selectLeftMenu", method = RequestMethod.GET)
    public JsonResult<List<MenuVO>> selectLeftMenu() {
        List<MenuVO> list = userMenuService.selectLeftMenu();

        return JsonResult.success(null, list);
    }

    @ApiOperation("查询用户菜单树")
    @RequestMapping(path = "/selectMenuTree", method = RequestMethod.GET)
    public JsonResult<List<Tree<Void>>> selectMenuTree() {
        List<Tree<Void>> list = userMenuService.selectMenuTree();

        return JsonResult.success(null, list);
    }

    @FeignService
    @ApiOperation("控制菜单显示")
    @RequestMapping(path = "/display", method = RequestMethod.POST)
    public JsonResult<Void> display(@RequestBody @Validated MenuTreeDTO menuTreeDTO) {
        userMenuService.display(menuTreeDTO);

        return JsonResult.success(CustomMessages.OPERATE_SUCCESS);
    }

    @FeignService
    @ApiOperation("设置菜单排序")
    @RequestMapping(path = "/updateSort", method = RequestMethod.POST)
    public JsonResult<Void> updateSort(@RequestBody @Validated UserMenuDTO userMenuDTO) {
        userMenuService.updateSort(userMenuDTO);

        return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
    }

    @FeignService
    @ApiOperation("查询用户的侧栏菜单")
    @RequestMapping(path = "/getLeftMenu", method = RequestMethod.GET)
    public JsonResult<List<MenuVO>> getLeftMenu(
            @ApiParam(name = "id", value = "用户ID", required = true)
            @NotBlank(message = "用户" + CustomMessages.ID_NOT_NULL) String userId) {
        List<MenuVO> list = userMenuService.getLeftMenu(userId);

        return JsonResult.success(null, list);
    }

    @FeignService
    @ApiOperation("查询用户菜单树")
    @RequestMapping(path = "/getMenuTree", method = RequestMethod.GET)
    public JsonResult<List<Tree<Void>>> getMenuTree(
            @ApiParam(name = "id", value = "用户ID", required = true)
            @NotBlank(message = "用户" + CustomMessages.ID_NOT_NULL) String userId) {
        List<Tree<Void>> list = userMenuService.getMenuTree(userId);

        return JsonResult.success(null, list);
    }

}